package com.zch.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.base.dialect.PaginationSupport;
import com.base.dao.model.Ret;
import com.base.dao.model.Grid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang.StringUtils;
import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;
import com.item.service.MessageService;

import org.springframework.beans.factory.annotation.Autowired;
import com.base.action.CoreController;
import com.zch.dao.model.CompanyRegister;
import com.zch.dao.model.ManagerFollow;
import com.zch.dao.model.ManagerFollowExample;
import com.zch.dao.model.ProjectMessage;
import com.zch.daoEx.model.ManagerFollowEx;
import com.zch.service.ManagerFollowService;
import com.zch.service.ProjectMessageService;

@RequestMapping("managerFollow")
@Controller
@LoginFilter
public class ManagerFollowController extends CoreController{

	@Autowired
	private ManagerFollowService managerFollowService;
	@Autowired
    private ProjectMessageService projectMessageService;
	@Autowired
    private MessageService messageService;
	@RequestMapping("/list")
	@ResponseBody
	public String list(Integer page, Integer rows, String name){
		PaginationSupport.byPage(page, rows);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		List<ManagerFollowEx> list = managerFollowService.selectlist(map);
		return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
	}

	@RequestMapping("/save")
	@ResponseBody
	public String save(ManagerFollow managerFollow){
		if (StringUtils.isBlank(managerFollow.getId())){
			managerFollowService.insert(managerFollow);
		}else{
			managerFollowService.updateByPrimaryKeySelective(managerFollow);
			ManagerFollow managerFollow2 = managerFollowService.selectByPrimaryKey(managerFollow.getId());
			
			ProjectMessage ex = new ProjectMessage();
            ex.setId(managerFollow2.getProjectId());
            ex.setIsEnable(1);
            projectMessageService.updateByPrimaryKeySelective(ex);
            
            managerFollow = managerFollowService.selectByPrimaryKey(managerFollow.getId());
            String projectId  =managerFollow.getProjectId();
            
            ProjectMessage projectMessage = projectMessageService.selectByPrimaryKey(projectId);
            String pushContent = "";
            if (projectMessage.getIsAgree() == 2){
                pushContent = "审核未通过!原因"+projectMessage.getNotAgreeReason();
                
                projectMessage.setNotAgreeReason(managerFollow.getNotAgreeReason());
                projectMessage.setIsEnable(1);
                projectMessage.setIsAgree(2);
                projectMessageService.updateByPrimaryKeySelective(projectMessage);
                //推送给项目发布人
                messageService.findMessage(projectMessage.getTitle(), pushContent, projectMessage.getUserId(), 1);
                //推送给项目可行性分析人
                messageService.findMessage(projectMessage.getTitle(), pushContent, projectMessage.getAnalyzeMaster(), 1);
                //推送给项目对接人
                messageService.findMessage(projectMessage.getTitle(), pushContent, projectMessage.getConnectMaster(), 1);
                //推送给项目公司注册人
                messageService.findMessage(projectMessage.getTitle(), pushContent, projectMessage.getRegisterMaster(), 1);
            }
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/findById")
	@ResponseBody
	public String find(String id){
		ManagerFollow managerFollow = managerFollowService.selectByPrimaryKey(id);
		return JSONUtils.serialize(managerFollow);
	}

	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String str : ids){
			managerFollowService.deleteByPrimaryKey(str);
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/update")
	@ResponseBody
    public String update(ManagerFollow managerFollow){
        this.managerFollowService.updateByPrimaryKeySelective(managerFollow);
        return JSONUtils.serialize(new Ret(0));
    }
	
    @RequestMapping("/saveResolveMaster")
    @ResponseBody
    public String saveResolveMaster(String  userId,String id){
        if (StringUtils.isNotBlank(userId)){
            int result = managerFollowService.updateResolveMaster(userId,id);
            if(result == 1){
                ManagerFollow managerFollow = managerFollowService.selectByPrimaryKey(id);
                ProjectMessage projectMessage = projectMessageService.selectByPrimaryKey(managerFollow.getProjectId());
                //推送给发布人
                messageService.findMessage(projectMessage.getTitle(), "审核通过！", managerFollow.getUserId(), 2);
                
                //推送给可行性分析人员
                String pushContent2 = "您有一个项目需要进行运营跟踪！";
                messageService.findMessage(projectMessage.getTitle(), pushContent2, userId, 2);
            }
        }
        return JSONUtils.serialize(new Ret(0));
    } 
}